#!/bin/bash
#description: auto install jumpserver on centos7.4
IP=192.168.90.30
VER=1.0.0

function init(){
        #配置epel源
        cd /etc/yum.repos.d/ && [ ! -f epel.repo ] &&  yum -y install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
        #防火墙配置
        systemctl enable firewalld
        systemctl start firewalld
        firewall-cmd --zone=public --add-port=80/tcp --permanent
        firewall-cmd --zone=public --add-port=22/tcp --permanent
        firewall-cmd --zone=public --add-port=2222/tcp --permanent
        firewall-cmd --zone=public --add-port=5000/tcp --permanent
        firewall-cmd --zone=public --add-port=8081/tcp --permanent
        firewall-cmd --zone=public --add-port=8080/tcp --permanent
	firewall-cmd --reload
	sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/sysconfig/selinux
	setenforce 0
        #修改字符集
        localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8
        export LC_ALL=zh_CN.UTF-8
        echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf
	#更改pip的镜像源
	[ ! -d /root/.pip/ ] && mkdir /root/.pip
	cat >/root/.pip/pip.conf <<EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
EOF
}
#准备python3和python虚拟机环境
function Python_virutal(){
	#安装依赖包
        yum -y install python-devel zlib wget autoconf make readline-devel python-pip sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git
	#编译安装Python3.6.1
	wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
	tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1
	./configure && make && make install
	#建议python虚拟环境
	cd /opt && python3 -m venv louis_py3
	source /opt/louis_py3/bin/activate
	#此项仅为懒癌晚期的人员使用，防止运行 Jumpserver 时忘记载入 Python 虚拟环境导致程序无法运行。使用autoenv
	git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
	echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
	source ~/.bashrc
}

#安装jumpserver
function install_jumpserver(){
	#安装redis、redis、mariadb并设置好数据库及权限
        yum -y install redis nginx mariadb mariadb-server mariadb-devel
        systemctl enable redis
        systemctl enable mariadb
        systemctl start redis
        systemctl start mariadb
	systemctl enable nginx
        systemctl start nginx
	if [ `ps -ef |grep nginx|grep -v grep|wc -l` -lt 1 -a `ps -ef|grep redis|grep -v grep|wc -l` -lt 1 -a `ps -ef|grep mysqld|grep -v grep|wc -l` -lt 1 ];then
		echo "nginx or mysqld service start fail"
		exit 2
	fi
		
	#建jumpserver数据库，并授权
	mysql -e "grant all on *.* to 'root'@'localhost' identified by 'Aa123321';flush privileges;"
        mysql -h localhost -u root -pAa123321 -e "create database jumpserver default charset 'utf8';grant all on jumpserver.* to 'jumpserver'@'127.0.0.1' identified by 'Jump123321';flush privileges;"
	#下载jumpserver
	cd /opt && wget https://codeload.github.com/jumpserver/jumpserver/tar.gz/${VER} && tar xf ${VER}
	ln -sv /opt/jumpserver-${VER} /opt/jumpserver
	#安装依赖的rpm包
	cd  /opt/jumpserver-${VER}/requirements && yum -y install $(cat rpm_requirements.txt)
	#安装python库依赖
        pip install --upgrade pip
	cd /opt/jumpserver-${VER}/requirements/
	sed -i "s/Django==1.11/django==1.11/" requirements.txt
	sed -i "s/boto3==1.6.4/boto3==1.6.5/" requirements.txt
	sed -i "s/botocore==1.9.4/botocore==1.9.5/" requirements.txt
	sed -i "s/certifi==2017.11.5/certifi==2018.1.18/" requirements.txt
	sed -i "s/pytz==2017.3/pytz==2018.3/" requirements.txt
	pip install -r  requirements.txt
	#修改Jumpserver文件
	 cd /opt/jumpserver && cp config_example.py config.py
        sed -i "s/DEBUG = True/DEBUG = False/" config.py
        sed -i "s/OG_LEVEL = 'DEBUG'/OG_LEVEL = 'WARNING'/" config.py
        #连接mysql数据库配置
        sed -i "s/DB_ENGINE = 'sqlite3'/# &/" config.py
        sed -i "s/DB_NAME = os/# &/" config.py
        sed -i "s/# DB_ENGINE = 'mysql'/DB_ENGINE = 'mysql'/" config.py
        sed -i "s/# DB_HOST = '127.0.0.1'/DB_HOST = '127.0.0.1'/" config.py
        sed -i "s/# DB_PORT = 3306/DB_PORT = 3306/" config.py
        sed -i "s/# DB_USER = 'root'/DB_USER = 'jumpserver'/" config.py
        sed -i "s/# DB_PASSWORD = ''/DB_PASSWORD = 'Jump123321'/" config.py
        sed -i "s/# DB_NAME = 'jumpserver'/DB_NAME = 'jumpserver'/" config.py
        #Django运行的端口和容器，把0.0.0.0改成127.0.0.1仅仅允许自己访问
        sed -i "s/HTTP_BIND_HOST = '0.0.0.0'/HTTP_BIND_HOST = '127.0.0.1'/" config.py
	#生成数据库表结构和初始化数据
	cd /opt/jumpserver/utils && bash make_migrations.sh
	#启动jumpserver
	cd /opt/jumpserver 
	nohup python run_server.py all &
}
function install_coco(){
	#新开一个终端，连接测试机，别忘了 source /opt/py3/bin/activate
	cd /opt/ && source /opt/louis_py3/bin/activate
	#git clone https://github.com/jumpserver/coco.git && cd coco && git checkout master
	wget https://codeload.github.com/jumpserver/coco/tar.gz/1.0.0 && tar xf 1.0.0
	
	#echo "source /opt/louis_py3/bin/activate" > /opt/coco/.env
        #安装依赖的rpm包
	cd /opt/coco-1.0.0 && yum -y install $(cat /opt/coco-1.0.0/requirements/rpm_requirements.txt)
	cd /opt/coco/requirements/
	sed -i "s/pytz==2017.3/pytz==2017.2/" requirements.txt
	sed -i "s/pyte==0.7.0/pyte==0.5.2/" requirements.txt
        pip install -r /opt/coco-1.0.0/requirements/requirements.txt
	#修改配置文件	
	cd /opt/coco-1.0.0 && cp conf_example.py conf.py
	sed -i "s/# NAME = "localhost"/# NAME = "alsproject"/" conf.py
	#启动coco服务
	cd /opt/coco-1.0.0/ && nohup python run_server.py &
}
function install_web_terminal(){
	#访问（https://github.com/jumpserver/luna/releases）下载对应版本的 release 包，直接解压，不需要编译
	cd /opt/
	wget https://github.com/jumpserver/luna/releases/download/v1.0.0/luna.tar.gz
	tar xvf dist.tar.gz
	mv dist luna
	#安装windows支持的组件
	yum remove docker-latest-logrotate  docker-logrotate  docker-selinux dockdocker-engine
	yum install -y yum-utils   device-mapper-persistent-data   lvm2
	#添加docker官方源
	yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
	yum makecache fast
	yum install docker-ce
	systemctl start docker
#	启动 Guacamole 
	#这里所需要注意的是 guacamole 暴露出来的端口是 8081，若与主机上其他端口冲突请自定义、修改下面 docker run 里的 JUMPSERVER_SERVER 参数，填上 Jumpserver 的 url 地址, 启动成功后去 Jumpserver 会话管理-终端管理（http://192.168.244.144:8080/terminal/terminal/）接受[Gua]开头的一个注册，如果页面显示不正常可以等部署完成后再处理
	docker run --name jms_guacamole -d \
  		-p 8081:8080 -v /opt/guacamole/key:/config/guacamole/key \
 		-e JUMPSERVER_KEY_DIR=/config/guacamole/key \
  		-e JUMPSERVER_SERVER=http://$IP \
  		registry.jumpserver.org/public/guacamole:latest
}
function config_nginx(){
	cd /etc/nginx/conf.d/ 
	cat > jumpserver.conf <<EOF
server {
    listen 80;

    proxy_set_header X-Real-IP \$remote_addr;
    proxy_set_header Host \$host;
    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;

    location /luna/ {
        try_files \$uri / /index.html;
        alias /opt/luna/;
    }

    location /media/ {
        add_header Content-Encoding gzip;
        root /opt/jumpserver/data/;
    }

    location /static/ {
        root /opt/jumpserver/data/;
    }

    location /socket.io/ {
        proxy_pass       http://127.0.0.1:5000/socket.io/;  # 如果coco安装在别的服务器，请填写它的ip
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location /guacamole/ {
        proxy_pass       http://127.0.0.1:8081/;  # 如果docker安装在别的服务器，请填写它的ip
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection \$http_connection;
        access_log off;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;  # 如果jumpserver安装在别的服务器，请填写它的ip
    }
}
EOF
	#重启nginx服务
	systemctl restart nginx
}


DATE=$(date +%F)
KERNEL_VERSION=$(uname -r)
HOSTNAME=$(hostname)
cat <<EOF
-----------------------------------------------------------------
                auto Install jumpserver
----------------------------------------------------------------
| DATE                   :  $DATE
| IP                     :  $SERVERIP
| KERNEL_VERSION         :  ${KERNEL_VERSION}
| HOSTNAME               :  $HOSTNAME
---------------------------------------------------------------
|*********Please Enter your Choice:***************************|
(1) Init system and Install python3.6
(2) Auto install Jumpserver
(3) Auto install Coco
(4) Auto install Web_terminal
(5) Configure Nginx to integrate the components
(6) quit
EOF
read -p "please enter your choice:" input
case $input in
1)
   init
   Python_virutal
   ;;
2)
   install_jumpserver
   ;;
3)
   install_coco
   ;;
4)
   install_web_terminal
   ;;
5)
   config_nginx
   ;;
6)
  exit 1
esac

